package com.wtwd.wtwddevice.service.impl;


import com.wtwd.device.util.DateUtils;
import com.wtwd.wtwddevice.mapper.DeviceBloodOxygenMapper;
import com.wtwd.wtwddevice.model.entity.DeviceBloodOxygen;
import com.wtwd.wtwddevice.model.entity.DeviceBloodOxygenTotal;
import com.wtwd.wtwddevice.model.entity.DeviceHeartRateTotal;
import com.wtwd.wtwddevice.service.IDeviceBloodOxygenService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.wtwddevice.service.IDeviceBloodOxygenTotalService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2024-05-29
 */
@Service
public class DeviceBloodOxygenServiceImpl extends ServiceImpl<DeviceBloodOxygenMapper, DeviceBloodOxygen> implements IDeviceBloodOxygenService {

    @Resource
    private IDeviceBloodOxygenTotalService deviceBloodOxygenTotalService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insert(DeviceBloodOxygen deviceBloodOxygen) {
        DeviceBloodOxygenTotal deviceBloodOxygenTotal = getSynchronizationCache(deviceBloodOxygen);
        deviceBloodOxygen.setDeviceBloodOxygenTotalId(deviceBloodOxygenTotal.getId());
        boolean save = save(deviceBloodOxygen);
        if(save){
            deviceBloodOxygenTotal.setLastInfoId(deviceBloodOxygen.getId());
            deviceBloodOxygenTotalService.updateById(deviceBloodOxygenTotal);
        }
        return save;
    }

    @Override
    public DeviceBloodOxygenTotal getSynchronizationCache(DeviceBloodOxygen deviceBloodOxygen) {
        DeviceBloodOxygenTotal deviceBloodOxygenTotal = deviceBloodOxygenTotalService.getDeviceBloodOxygenTotal(deviceBloodOxygen.getDeviceId(), DateUtils.convertDateFormat(deviceBloodOxygen.getTime()));
        if (deviceBloodOxygenTotal != null) {
            deviceBloodOxygenTotal.setMin(deviceBloodOxygenTotal.getMin() > deviceBloodOxygen.getValue() ? deviceBloodOxygen.getValue() : deviceBloodOxygenTotal.getMin());
            deviceBloodOxygenTotal.setMax(deviceBloodOxygenTotal.getMax() < deviceBloodOxygen.getValue() ? deviceBloodOxygen.getValue() : deviceBloodOxygenTotal.getMax());
            deviceBloodOxygenTotal.setTotal(deviceBloodOxygenTotal.getTotal() == null ? deviceBloodOxygen.getValue() : (deviceBloodOxygenTotal.getTotal() + deviceBloodOxygen.getValue()));
            deviceBloodOxygenTotal.setRows(deviceBloodOxygenTotal.getRows() == null ? 1 : deviceBloodOxygenTotal.getRows() + 1);
        } else {
            deviceBloodOxygenTotal = new DeviceBloodOxygenTotal();
            BeanUtils.copyProperties(deviceBloodOxygen, deviceBloodOxygenTotal);
            deviceBloodOxygenTotal.setMin(deviceBloodOxygen.getValue());
            deviceBloodOxygenTotal.setMax(deviceBloodOxygen.getValue());
            deviceBloodOxygenTotal.setTotal(deviceBloodOxygen.getValue());
            deviceBloodOxygenTotal.setRows(1);
            deviceBloodOxygenTotal.setTime(DateUtils.convertDateFormat(deviceBloodOxygen.getTime()));
            deviceBloodOxygenTotalService.save(deviceBloodOxygenTotal);
        }
        return deviceBloodOxygenTotal;
    }
}
